package com.onepower.admin.core.common;


import com.onepower.core.exception.BizException;
import com.onepower.core.exception.SystemException;
import com.onepower.core.result.BaseResultCode;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
@Slf4j
public class ApiControllerAdvice {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult handleException(final HttpServletRequest request, Exception ex) {
        ex.printStackTrace();
        return ResultUtils.error();
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public CommonResult handleHttpRequestMethodNotSupportedException(Exception e) {
        return ResultUtils.error(BaseResultCode.REQUEST_METHOD_NOT_SUPPORTED);
    }


    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public CommonResult handleBizException(final HttpServletRequest request, final BizException e) {
        return ResultUtils.error(e.hashCode(), e.getMessage());
    }
}
